package com.qiall.omsserver.service.ssh.sftp;

import com.jcraft.jsch.ChannelSftp;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

public class SftpConfig {

    private SftpFactory sftpFactory;

    private GenericObjectPoolConfig<ChannelSftp> genericObjectPoolConfig;

    private SftpGenericObjectPool sftpGenericObjectPool;

    public SftpConfig(String userName, String password, int port, String host) {
        this.sftpFactory = new SftpFactory(host, port, userName, password);
        sftpPoolConfig();
        sftpPool();
    }

    private void sftpPoolConfig() {
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig<>();
        genericObjectPoolConfig.setMaxTotal(10);
        genericObjectPoolConfig.setMaxIdle(10);
        genericObjectPoolConfig.setMinIdle(5);
        this.genericObjectPoolConfig = genericObjectPoolConfig;
    }

    private void sftpPool() {
        this.sftpGenericObjectPool = new SftpGenericObjectPool(sftpFactory, genericObjectPoolConfig);
    }

    public SftpUtil sftpUtil() {
        return new SftpUtil(sftpGenericObjectPool);
    }

}
